﻿#include "ToggleCommand.h"

#include "../../Modules/ModulesManager.h"

void ToggleCommand_onInit(ICommand* toggleCMD) {
	toggleCMD->aliasList.reserve(ModuleManager::NameTable.size());
	for (auto& i : ModuleManager::NameTable)
		toggleCMD->aliasList.emplace_back_unchecked(stringview_Judge{i.first, nullptr}); // NameTable已经初始化完毕，string指针不会变动
}

bool ToggleCommand_onExecute(const fast_io::vector<std::u8string>& args) {
	if (GameData::getLocalPlayer() == nullptr) [[unlikely]]
		return true;

	if (args.size() != 2) 
		return false;

	if (args[1].empty())
		return false;

	std::u8string name = args[1];

	std::ranges::for_each(name, [](char8_t& str) { str = fast_io::char_category::to_c_lower(str); });

	auto modOpt = ModuleManager::getModuleByName(name);

#if 0
	if (modOpt == &TestModule) {
		switch (SettingManager::languageget) {
		case 0:
		default:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::RED,
											 u8"Invalid module!");
			break;
		case 1:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::RED,
											 u8"无效模块");
			break;
		case 2:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::RED,
											 u8"無效模塊");
			break;
		case 3:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::RED,
											 u8"無効なモジュール");
			break;
			std::unreachable();
		}

		return true;
	}
#endif  // 0

	using std::operator""sv;

	if (modOpt) {
		modOpt->toggle();
		switch (SettingManager::languageget) {
		case 0:
		default:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::WHITE,
											 u8"Module ",
											 Utils::TextColor::YELLOW,
											 name,
											 Utils::TextColor::WHITE,
											 u8" is now ",
											 Utils::TextColor::YELLOW,
											 modOpt->isEnabled() ? u8"ENABLED"sv : u8"DISABLED"sv);
			break;
		case 1:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::WHITE,
											 u8"模块 ",
											 Utils::TextColor::YELLOW,
											 name,
											 Utils::TextColor::WHITE,
											 u8" 现在",
											 Utils::TextColor::YELLOW,
											 modOpt->isEnabled() ? u8"开启"sv : u8"关闭"sv);
			break;
		case 2:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::WHITE,
											 u8"模塊 ",
											 Utils::TextColor::YELLOW,
											 name,
											 Utils::TextColor::WHITE,
											 u8" 現在",
											 Utils::TextColor::YELLOW,
											 modOpt->isEnabled() ? u8"開啟"sv : u8"關閉"sv);
			break;
		case 3:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::WHITE,
											 u8"モジュール ",
											 Utils::TextColor::YELLOW,
											 name,
											 Utils::TextColor::WHITE,
											 modOpt->isEnabled() ? u8" がオンになりました"sv : u8" が閉じられました"sv);
			break;
			std::unreachable();
		}
	} else {
		switch (SettingManager::languageget) {
		case 0:
		default:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::RED,
											 u8"Could not find Module with name: ",
											 name);
			break;
		case 1:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::RED,
											 u8"找不到名为 ",
											 name,
											 u8" 的模块");
			break;
		case 2:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::RED,
											 u8"找不到名為 ",
											 name,
											 u8" 的模塊");
			break;
		case 3:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::RED,
											 name,
											 u8" という名前のモジュールが見つかりませんでした");
			break;
			std::unreachable();
		}
	}
	return true;
}

ICommand ToggleCommand{
	u8"toggle",
	u8".toggle <Module Name>",
	{u8"Toggles a module", u8"切换模块", u8"切換模塊", u8"スイッチモジュール"},
	{},
	fast_io::vector<aliasList_struct>{},  // If empty must use fast_io::vector<aliasList_struct>{}
	&ToggleCommand_onInit,
	&ToggleCommand_onExecute};
